import MySql.User;
import MySql.UserManager;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class CheckServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
//        out.println("欢迎");
        String name=request.getParameter("uname");
        String pwd=request.getParameter("upwd");
        String check=request.getParameter("check");
        String auto=request.getParameter("auto");
        String vali=request.getSession().getAttribute("vali").toString();
        String uvali=request.getParameter("vali");

        if(!uvali.equals(vali)){
            out.println("验证码错误,请重新登录,2s内将跳转页面到登录界面");
            response.setHeader("Refresh","2;url=login.jsp");
        }else{
            try{
                Class.forName("com.mysql.jdbc.Driver");

                User u=new User();
                u.setUsername(name);
                u.setPasswd(pwd);
                UserManager userManager=new UserManager();
                if(userManager.userLogin(u)){
                    if(check!=null&&check.equals("on")){
                        Cookie cookie =new Cookie("name",name);
                        Cookie cookie1 =new Cookie("pwd",pwd);
                        cookie.setMaxAge(60*60*60);
                        cookie1.setMaxAge(60*60*60);
                        cookie.setPath(request.getContextPath() + "/");
                        cookie1.setPath(request.getContextPath() + "/");
                        response.addCookie(cookie);
                        response.addCookie(cookie1);
                    }
                    if(auto!=null&&auto.equals("on")){
                        Cookie autocookie =new Cookie("auto",auto);
                        autocookie.setPath(request.getContextPath() + "/");
                        autocookie.setMaxAge(10);
                        response.addCookie(autocookie);
                    }

                    HttpSession session=request.getSession();
                    session.setAttribute("name",name);


                    out.println(name+"登录成功，欢迎你，2秒内会继续访问");

                    if(name.equals("admin")) response.setHeader("Refresh","2;url=manager.jsp");
                    else response.setHeader("Refresh","2;url=PetHouse.jsp");
//                response.sendRedirect("success.go");

                }else{
                    out.println("账号或密码有误");
                    response.setHeader("Refresh","2;url=login.jsp");
                }

            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



    }
}
